package com.ygqh.customs.handler;

import com.ygqh.customs.config.CustomsConfig;
import com.ygqh.customs.domain.GoodsInfo;
import com.ygqh.customs.domain.PayExchangeInfoList;
import com.ygqh.customs.model.OrderData;
import com.ygqh.customs.model.OrderDetailData;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * 〈〉
 *
 * @author guohao
 * @date 2019/3/18 18:48
 * @since 1.0.0
 */
@Slf4j
public class PayExchangeInfoListHandler {

    private final static String M_PRODYCT_PAGE_URL = "http://m.yiigoo.com/product/product.html?id=%s";
    private OrderData orderData;

    private CustomsConfig customsConfig;

    public PayExchangeInfoListHandler(OrderData orderData, CustomsConfig customsConfig) {
        this.orderData = orderData;
        this.customsConfig = customsConfig;
    }

    public List<PayExchangeInfoList> buildPayExchangeInfoLists() {
        List<PayExchangeInfoList> infoLists = new ArrayList<>();
        PayExchangeInfoList infoList = new PayExchangeInfoList();
        infoList.setOrderNo(orderData.getPaymentCode());

        List<GoodsInfo> goodsInfo = new ArrayList<>();
        for (OrderDetailData detailData : orderData.getDetailList()) {
            GoodsInfo info = new GoodsInfo();
            info.setGname(detailData.getProductName());
            info.setItemLink(String.format(M_PRODYCT_PAGE_URL, detailData.getProductId()));
            goodsInfo.add(info);
        }
        infoList.setGoodsInfo(goodsInfo);
        setRecpInfo(infoList);
        infoLists.add(infoList);
        return infoLists;
    }

    /**
     * 设置收款企业信息
     */
    private void setRecpInfo(PayExchangeInfoList infoList) {

        if ("Small".equals(orderData.getAppType())) {
            infoList.setRecpAccount(customsConfig.getRecpAccountS());
        } else if ("WeChat".equals(orderData.getAppType())) {
            infoList.setRecpAccount(customsConfig.getRecpAccountM());
        }
        infoList.setRecpCode(customsConfig.getRecpCode());
        infoList.setRecpName(customsConfig.getRecpName());

    }
}
